package cn.lbf.stuniversity.classroomequipment.service;

import cn.lbf.stuniversity.classroomequipment.dao.ClassroomEquipmentDao;
import cn.lbf.stuniversity.classroomequipment.entity.ClassroomEquipment;
import cn.lbf.stuniversity.classroomequipment.form.ClassroomEquipmentAddForm;
import cn.lbf.stuniversity.classroomequipment.form.ClassroomEquipmentDetailForm;
import cn.lbf.stuniversity.classroomequipment.form.ClassroomEquipmentForm;
import cn.lbf.stuniversity.constant.Code;
import cn.lbf.stuniversity.utils.PageUtil;
import cn.lbf.stuniversity.utils.ResultUtil;
import cn.lbf.stuniversity.vo.PageVO;
import cn.lbf.stuniversity.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClassroomEquipmentServiceImpl implements ClassroomEquipmentService {
    @Autowired
    private ClassroomEquipmentDao classroomEquipmentDao;

    public List<ClassroomEquipment> selectAll(){
        return classroomEquipmentDao.selectAll();
    }

    @Override
    public Result<Map<String, Object>> addNew(ClassroomEquipmentAddForm form) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("classroomEquipmentId",form.getClassroomEquipmentId());
        map.put("classroomName",form.getClassroomName());
        int ClassCount=classroomEquipmentDao.selectClassCountByIdAndName(map);
        System.out.println(map.get("classroomEquipmentId"));
        System.out.println(map.get("classroomName"));
        if(ClassCount>0){//如果查詢出來有同名的分類
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.HAS_EXISTS, "已有同名的课室!");
        }
        int i = classroomEquipmentDao.addNew(form);
        if(i>0){
            return new ResultUtil<Map<String, Object>>().setSuccessMsg("添加成功");
        }else{
            return new ResultUtil<Map<String, Object>>().setErrorMsg(Code.FAIL, "添加失败，请联系管理员!");
        }
    }

    @Override
    public Result<Page<ClassroomEquipment>> queryList(ClassroomEquipmentForm form) {
            // 1. 先查询数据（XML已经处理了分页）
            try{
                form.initOffSet();//
                List<ClassroomEquipment> list = classroomEquipmentDao.selectClassByIdAndName(form);
                long count = classroomEquipmentDao.queryCount(form);
                PageVO pageVO = new PageVO(form.getPageNumber(), form.getPageSize());
                PageImpl<ClassroomEquipment> page = new PageImpl<>(list, PageUtil.initPage(pageVO), count);
                return new ResultUtil<Page<ClassroomEquipment>>().setData(page);
            }catch (Exception e){
                e.printStackTrace();
                return new ResultUtil<Page<ClassroomEquipment>>().setErrorMsg("查询有误 请联系管理员");
            }
        }

    @Override
    public Result<String> del(String id) {
        ClassroomEquipment classroomEquipment = classroomEquipmentDao.selectById(id);
        if(classroomEquipment==null){
            return new ResultUtil<String>().setErrorMsg(Code.NOT_EXISTS, "分类不存在");
        }
        int remove = classroomEquipmentDao.deleteByUpdateId(id);
        if (remove > 0) {
            return new ResultUtil<String>().setSuccessMsg("删除成功");
        }else {
            return new ResultUtil<String>().setErrorMsg(Code.FAIL,"删除失败 请联系管理员");
        }
    }

    @Override
    public Result<String> updateByClassId(ClassroomEquipmentDetailForm form) {
        ClassroomEquipment classroomEquipment = classroomEquipmentDao.selectById(form.getClassroomEquipmentId());
        if(classroomEquipment ==null){
            return new ResultUtil<String>().setErrorMsg(Code.FAIL,"更新失败 查无此教室");
        }
        int updateNum = classroomEquipmentDao.updateByClassroomEquipmentId(form);
        if (updateNum > 0) {
            return new ResultUtil<String>().setSuccessMsg("更新成功!");
        }
        return new ResultUtil<String>().setErrorMsg("更新失败!");
    }

}
